package com.zxt.content.copywriting.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxt.content.common.utils.StringUtils;
import com.zxt.content.common.utils.ZXTCollectionsUtil;
import com.zxt.content.copywriting.mapper.PrivateCopywritingMapper;
import com.zxt.content.copywriting.model.PrivateCopywriting;
import com.zxt.content.copywriting.req.PrivateCopywritingDTO;
import com.zxt.content.copywriting.req.PrivateCopywritingQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用逻辑层-私有文案管理类
 *
 * @author zxt
 */
@Slf4j
@Service
public class PrivateCopywritingManager extends ServiceImpl<PrivateCopywritingMapper, PrivateCopywriting> {

    public List<PrivateCopywriting> queryList() {
        return list();
    }

    public PrivateCopywriting randGetOneByUserId(Long userId) {
        return baseMapper.randGetOneByUserId(userId);
    }

    public List<PrivateCopywriting> themesList(PrivateCopywritingDTO dto) {
        LambdaQueryWrapper<PrivateCopywriting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PrivateCopywriting::getTheme, dto.getTheme());
        queryWrapper.and(s -> s.eq(PrivateCopywriting::getUserId, dto.getUserId()));
        return list(queryWrapper);
    }

    public List<PrivateCopywriting> copywritersList(PrivateCopywritingDTO dto) {
        LambdaQueryWrapper<PrivateCopywriting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PrivateCopywriting::getTheme, dto.getTheme());
        queryWrapper.and(query -> {
            query.eq(PrivateCopywriting::getUserId, dto.getUserId());
            query.ne(PrivateCopywriting::getId, dto.getId());
        });
        return list(queryWrapper);
    }

    public Page<PrivateCopywriting> pageList(Page<PrivateCopywriting> page, PrivateCopywritingQueryDTO privateCopywritingDTO, List<Long> copywritingId) {
        LambdaQueryWrapper<PrivateCopywriting> wrapper = PrivateCopywriting.gw();
        wrapper.eq(PrivateCopywriting::getUserId, privateCopywritingDTO.getUserId());
        wrapper.in(ZXTCollectionsUtil.isNotEmpty(copywritingId), PrivateCopywriting::getId, copywritingId);
        wrapper.eq(StringUtils.isNotEmpty(privateCopywritingDTO.getAuthor()), PrivateCopywriting::getAuthor, privateCopywritingDTO.getAuthor());
        wrapper.like(StringUtils.isNotEmpty(privateCopywritingDTO.getTheme()), PrivateCopywriting::getTheme, privateCopywritingDTO.getTheme());
        wrapper.orderByDesc(PrivateCopywriting::getCreateTime);
        return page(page, wrapper);
    }


    public void updateStatusByIds(List<Long> privateCopywritingIds, Byte copywritingStatus) {
        LambdaUpdateWrapper<PrivateCopywriting> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PrivateCopywriting::getCopywritingStatus, copywritingStatus);
        wrapper.in(PrivateCopywriting::getId, privateCopywritingIds);
        update(wrapper);
    }
}
